{% extends 'layout.html' %}
{% load static %}
{% block lk %}
    {% load static %}
    <title>商品详情</title>
    <link rel="stylesheet" type="text/css" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}">
{% endblock %}

{% block content %}
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">

            <h4 class="modal-title" id="myModalLabel"></h4>
          </div>
          <div class="modal-body">
              <div class="panel panel-default">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <form id="form0">
                        <div class="clearfix">
                            {% for item in form %}
                                <div class="col-xs-12">
                                    <div class="form-group" style="position: relative">
                                        <label>{{ item.label }}</label>
                                        {{ item }}
                                        <span class="error-msg" style="color: #b92c28;position: absolute;"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
            <button  id="btnSave" type="button" class="btn btn-primary">提 交</button>
          </div>
        </div>
      </div>
    </div>
	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部商品分类</h1>
				<span></span>
				<ul class="subnav">
					<li><a href="http://127.0.0.1:8000/type/list/1/" class="fruit">新鲜水果</a></li>
					<li><a href="http://127.0.0.1:8000/type/list/2/" class="seafood">海鲜水产</a></li>
					<li><a href="http://127.0.0.1:8000/type/list/3/" class="meet">猪牛羊肉</a></li>
					<li><a href="http://127.0.0.1:8000/type/list/4/" class="egg">禽类蛋品</a></li>
					<li><a href="http://127.0.0.1:8000/type/list/5/" class="vegetables">新鲜蔬菜</a></li>
					<li><a href="http://127.0.0.1:8000/type/list/6/" class="ice">速冻食品</a></li>
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="/home/">首页</a></li>
			</ul>
		</div>
	</div>

	<div class="breadcrumb">
		<a href="http://127.0.0.1:8000/home/">全部分类</a>
		<span>></span>
		<a href="http://127.0.0.1:8000/type/list/{{ goods_data.category_id }}/0/">{{ goods_data.category }}</a>
		<span>></span>
		<a href="#">商品详情</a>
	</div>

	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img style="width: 400px;height: 350px" src="/static/img/{{ goods_data.img }}"></div>

		<div class="goods_detail_list fr">
			<h3>{{ goods_data.title }}</h3>
			<p></p>
			<div class="prize_bar">
				<span class="show_pirze" >¥<em>{{ goods_data.price }}</em></span>
				<span class="show_unit">单  位：500g</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input type="text" class="num_show fl"  id="num" value="1">
					<a href="javascript:;" class="add fr" id="add" price="{{ goods_data.price}}">+</a>
					<a href="javascript:;" class="minus fr" id="minus" price="{{ goods_data.price}}">-</a>
				</div>
			</div>
			<div class="total">总价：<em id="total">{{ goods_data.price }}</em><em>元</em></div>
			<div class="operate_btn">
				<a href="javascript:;" class="buy_btn" id="purchase">立即购买</a>
				<a href="javascript:;" class="add_cart" id="add_cart" uid="{{ goods_data.id }}">加入购物车</a>
			</div>
		</div>
	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul>
                    {% for data in new_goods_data %}
                        <li>
                            <a href="http://127.0.0.1:8000/detail/{{ data.id }}/"><img src="/static/img/{{ data.img }}"></a>
                            <h4><a href="#">{{ data.title }}</a></h4>
                            <div class="prize">￥{{ data.price }}</div>
                        </li>
                    {% endfor %}
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active">商品介绍</li>
			</ul>

			<div class="tab_content">
				<dl>

					<dt>商品详情：</dt>
					<dd>{{ goods_data.introduction }}</dd>

				</dl>
                <dl>
                    <dt>商品评论区：   <span style="float: right">评论数:{{ comment_num}}</span></dt>
                    {% for com in comment_data %}
					    <dd>{{ com.user }}：{{ com.content }}</dd>
                    {% endfor %}

				</dl>
                <dl>
                    <input id="btnAdd" type="button" value="评论" class="btn btn-primary" style="background-color: #4cae4c;border-color: white;float: right">
                </dl>
			</div>
		</div>
	</div>
{% endblock %}

{% block js %}
    <div class="add_jump"></div>
    <script src="{% static 'Js/js1.txt.js' %}"></script>
    <script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
	<script type="text/javascript">

        $(function(){
            bindBtnAddEvent();
            bindBtnSaveEvent();
        })


        var y=1;
        $("#num").val(y);

		var $add_x = $('#add_cart').offset().top;
		var $add_y = $('#add_cart').offset().left;

		var $to_x = $('#show_count').offset().top;
		var $to_y = $('#show_count').offset().left;

        var y= parseFloat($('#num').val());
        var price;
        var uid;

        $("#add").click(function (){
            y++
            $("#num").val(y);
            price = parseFloat($(this).attr("price"));
            document.getElementById("total").innerHTML=(y * price).toFixed(2);

        })
        $("#minus").click(function (){
            if (y-1<1){
                $("#num").val(y=1);
            }
            else{
                y--;
                $("#num").val(y);
                price = parseFloat($(this).attr("price"));
                document.getElementById("total").innerHTML=(y * price).toFixed(2);

            }

        })

        var key = $("#show_count").attr("num");

        $("#purchase").click(function (){
           location.href = "/add/single/order/{{ goods_data.id }}/"+y+"/"
        })

		$(".add_jump").css({'left':$add_y+80,'top':$add_x+10,'display':'block'})
		$('#add_cart').click(function(){
            $.ajax({
                url: '/add/cart/',
                type: "GET",
                data:
                    {
                        uid: $(this).attr("uid"),
                        num: y,
                    },
                dataType: "JSON",
            })

			$(".add_jump").stop().animate({

				'left': $to_y+7,
				'top': $to_x+7},
				"fast", function() {
					$(".add_jump").fadeOut('fast',function(){
                        key++;
						$('#show_count').html(key);
					});

			});
		})

        function bindBtnAddEvent(){
            $("#btnAdd").click(function (){
                //将正在编辑的ID置空
                //清空对话框的数据
                $('#form0')[0].reset();
                $('#myModalLabel').text("商品评论");
                $('#myModal').modal('show');
            })
        }
        function bindBtnSaveEvent(){
            $("#btnSave").click(function (){
                $(".error-msg").text("");
                doadd();
            })

        }
         function doadd(){
            $.ajax({
                url: '/comment/add/{{ goods_data.id }}/',
                type: 'post',
                data: $("#form0").serialize(),
                dataType: 'JSON',
                success: function (res){
                    if(res.status){
                        $("#form0")[0].reset();
                        $("#myModel").modal("show");
                        location.reload();
                    }
                    else{
                        $.each(res.error, function (name, data){
                                console.log(name,data);
                                $("#id_" + name).next().text(data[0])
                        })
                    }
                }
            })
         }




	</script>
{% endblock %}
